package com.luych.toolbox.common.event.base.service.publisher;

import com.luych.toolbox.common.event.base.service.WebSocketChatRemoteApplicationEvent;
import com.luych.toolbox.common.feign.param.gateway.center.WebSocketChat;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;

public class WebSocketChatPublisher {

    private final BusProperties busProperties;
    private final ApplicationEventPublisher applicationEventPublisher;

    public WebSocketChatPublisher(BusProperties busProperties, ApplicationEventPublisher applicationEventPublisher) {
        this.busProperties = busProperties;
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void send(String fromUserId, String toUserId, String message) {
        WebSocketChat chat = new WebSocketChat(fromUserId, message);
        WebSocketChatRemoteApplicationEvent event = new WebSocketChatRemoteApplicationEvent(busProperties.getId(), "**", toUserId, chat);
        applicationEventPublisher.publishEvent(event);
    }
}

